#include "stdafx.h"
#include "jdbmodule.h"
#include "strhelper.h"
#include "jappconfig.h"
#include <wx/msgdlg.h>


_variant_t vtMissing;
_bstr_t EmptyString;
//using namespace ado;
//namespace ado{
const wchar_t g_un[]=L"sa";
const wchar_t g_pw[]=L"9999";
//const wchar_t g_un[]=L"talentmy";
//const wchar_t g_pw[]=L"mygzz80519";
//talentmy; pwd = mygzz80519
const wchar_t g_constr_t[]=
L"Provider=SQLOLEDB;Persist Security Info=True;"
L"Data Source=%s,%d;"
L"Use Procedure for Prepare=1;"
L"Auto Translate=True;"
L"Packet Size=4096;"
L"Use Encryption for Data=False;"
L"Tag with column collation when possible=False;"
L"Initial Catalog=%s";


ado::ConnectionPtr ado::newHisCon(int nn)
{
    NetInfo * ni=JAppConfig::getCurNetInfo();
    if(ni){
        ado::ConnectionPtr emr;
        HRESULT hr=emr.CreateInstance("ADODB.Connection");
        if(FAILED(hr))
        {
            ShowError(wxT("创建Connect实例失败。"));
            return ado::ConnectionPtr();
        }
        std::wstring constr=str_format(g_constr_t,ni->netAddr.data(),ni->port,ni->dbName.data());
        emr->PutConnectionTimeout(3);
        try {
            emr->Open(constr.data(),g_un,g_pw,nn);
        } catch (_com_error & e) {
            ShowError(e);
            return ado::CommandPtr();
        }

        return emr;
    }else {
        return ado::ConnectionPtr();
    }
}

void ado::ShowError(_com_error &e)
{
    wxString err=wxString::Format(wxT("数据库错误，错误信息:\n%s"),e.ErrorMessage());
    ShowError(err);
}


void ado::ShowError(const wxString & errmsg)
{
    wxString err=wxString::Format(wxT("%s"),errmsg);
    wxMessageBox(err,wxT("应用程序错误"));
}

//}
const char* dataType2string(ADODB::DataTypeEnum tp)
{
    switch(tp)
    {
    case ADODB::adEmpty : return "adEmpty";
    case ADODB::adTinyInt : return "adTinyInt";
    case ADODB::adSmallInt : return "adSmallInt";
    case ADODB::adInteger : return "adInteger";
    case ADODB::adBigInt : return "adBigInt";
    case ADODB::adUnsignedTinyInt : return "adUnsignedTinyInt";
    case ADODB::adUnsignedSmallInt : return "adUnsignedSmallInt";
    case ADODB::adUnsignedInt : return "adUnsignedInt";
    case ADODB::adUnsignedBigInt : return "adUnsignedBigInt";
    case ADODB::adSingle : return "adSingle";
    case ADODB::adDouble : return "adDouble";
    case ADODB::adCurrency : return "adCurrency";
    case ADODB::adDecimal : return "adDecimal";
    case ADODB::adNumeric : return "adNumeric";
    case ADODB::adBoolean : return "adBoolean";
    case ADODB::adError : return "adError";
    case ADODB::adUserDefined : return "adUserDefined";
    case ADODB::adVariant : return "adVariant";
    case ADODB::adIDispatch : return "adIDispatch";
    case ADODB::adIUnknown : return "adIUnknown";
    case ADODB::adGUID : return "adGUID";
    case ADODB::adDate : return "adDate";
    case ADODB::adDBDate : return "adDBDate";
    case ADODB::adDBTime : return "adDBTime";
    case ADODB::adDBTimeStamp : return "adDBTimeStamp";
    case ADODB::adBSTR : return "adBSTR";
    case ADODB::adChar : return "adChar";
    case ADODB::adVarChar : return "adVarChar";
    case ADODB::adLongVarChar : return "adLongVarChar";
    case ADODB::adWChar : return "adWChar";
    case ADODB::adVarWChar : return "adVarWChar";
    case ADODB::adLongVarWChar : return "adLongVarWChar";
    case ADODB::adBinary : return "adBinary";
    case ADODB::adVarBinary : return "adVarBinary";
    case ADODB::adLongVarBinary : return "adLongVarBinary";
    case ADODB::adChapter : return "adChapter";
    case ADODB::adFileTime : return "adFileTime";
    case ADODB::adPropVariant : return "adPropVariant";
    case ADODB::adVarNumeric : return "adVarNumeric";
    case ADODB::adArray : return "adArray";
    default:
        assert(0);
    }
}


